home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Frameworks / DropShell Pascal / DSAppleEvents.p < prev    next >
Encoding:
Text File  |  1992-06-19  |  8.6 KB  |  275 lines  |  [TEXT/MPS ]

  1. {******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSAppleEvents.p
  5. **
  6. **   Description:    Generic AppleEvent handling routines
  7. **                    
  8. **                    This is the set of routines for handling the required Apple events.
  9. **                    You should NEVER have to modify this file!!!
  10. **                    Simply add code in DSUserProcs to the routines called by these.
  11. **
  12. *******************************************************************************
  13. **                       A U T H O R   I D E N T I T Y
  14. *******************************************************************************
  15. **
  16. **    Initials    Name
  17. **    --------    -----------------------------------------------
  18. **    LDR            Leonard Rosenthol
  19. **
  20. *******************************************************************************
  21. **                      R E V I S I O N   H I S T O R Y
  22. *******************************************************************************
  23. **
  24. **      Date        Time    Author    Description
  25. **    --------    -----    ------    ---------------------------------------------
  26. **    11/24/91            LDR        Added a handler for 'pdoc' as per DTS recommendation
  27. **                                This caused some reorg & userProc routine changes
  28. **                                I also created a new common AEVT doc extractor
  29. **                                Pass the new userDataHandle to odoc/pdoc routines
  30. **                                FailErr now uses the ErrorAlert routine to report probs
  31. **    10/30/91            LDR        Modified USES clause for new include & ifc'ed ThP
  32. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  33. **                                Added a bunch of comments for clarification
  34. **    04/09/91    00:04    LDR        Original Version
  35. **
  36. ******************************************************************************}
  37.  
  38. UNIT DSAppleEvents;
  39. INTERFACE
  40.  
  41. {$IFC THINK_Pascal}
  42.     USES
  43.         AppleEvents, DSGlobals, DSUtils, DSUserProcs;    {just the DropShell files}
  44. {$ELSEC}
  45.     USES
  46.     { First load standard interface files}
  47.         MemTypes, QuickDraw, 
  48.  
  49.     { Now include the stuff from OSIntf }
  50.         OSIntf, 
  51.  
  52.     { Now Include the stuff from ToolIntf.p }
  53.         ToolIntf, Packages, GestaltEqu, 
  54.  
  55.     { Then any OTHER Toolbox interfaces... }
  56.         Files, Aliases, AppleEvents,
  57.  
  58.     { And finally any files from DropShell }
  59.         DSGlobals, DSUtils, DSUserProcs;
  60. {$ENDC THINK_Pascal}
  61.     
  62. {---------------------}
  63. {Interface Definitions}
  64. {---------------------}
  65.  
  66.     PROCEDURE InitAEVTStuff;    
  67.     PROCEDURE DoHighLevelEvent( event: EventRecord ) ;    
  68.     
  69.     FUNCTION HandleOAPP( theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT ): OSErr ; 
  70.     FUNCTION HandleODOC( theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT ): OSErr ; 
  71.     FUNCTION HandlePDOC( theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT ): OSErr ; 
  72.     FUNCTION HandleQuit( theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT ): OSErr ; 
  73.  
  74. IMPLEMENTATION
  75.  
  76.     {$S Initialize}
  77.     { 
  78.         This routine does all initialization for AEM, including the
  79.         creation and then population of the dispatch table.
  80.     }
  81.     PROCEDURE InitAEVTStuff;
  82.     VAR
  83.         aevtErr: OSErr;
  84.  
  85.     BEGIN
  86.         aevtErr := noErr;
  87.  
  88.         IF (aevtErr = noErr) THEN
  89.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, 0, false);
  90.         IF (aevtErr = noErr) THEN
  91.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleODOC, 0, false);
  92.         IF (aevtErr = noErr) THEN
  93.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandlePDOC, 0, false);
  94.         IF (aevtErr = noErr) THEN
  95.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, 0, false);
  96.  
  97.         IF (aevtErr = noErr) THEN
  98.             InstallOtherEvents;
  99.  
  100.         IF (aevtErr <> noErr) THEN
  101.             ; {Report an error to the user, if you are so inclinded!}
  102.     END;
  103.     
  104.     
  105.     {$S Main}
  106.     { 
  107.         This routine is a utility routine for checking that all required 
  108.         parameters in the Apple event have been used.
  109.     }
  110.     
  111.     FUNCTION GotRequiredParams(theAppleEvent: AppleEvent): OSErr;
  112.     VAR
  113.         typeCode: DescType;
  114.         actualSize: Size;
  115.         err: OSErr;
  116.  
  117.     BEGIN
  118.         err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, typeCode, NIL, 0, actualSize); 
  119.         IF err = errAEDescNotFound THEN { we got all the required params: all is ok }
  120.             GotRequiredParams := noErr
  121.         ELSE IF err = noErr THEN
  122.             GotRequiredParams := errAEEventNotHandled
  123.         ELSE
  124.             GotRequiredParams := err;
  125.     END; { GotRequiredParams }
  126.     
  127.     {
  128.         This is another routine useful for showing debugging info.
  129.         
  130.         It calls the ErrorAlert routine from DSUtils to put up the 
  131.         error message.
  132.     }
  133.     PROCEDURE FailErr(err: OSErr);
  134.     BEGIN
  135.         IF err <> noErr THEN
  136.             ErrorAlert(kErrStringID, kAEVTErr, err);
  137.     END;
  138.     
  139.     
  140.  
  141.     {
  142.         This routine is the handler for the oapp (Open Application) event.
  143.         
  144.         It first checks the number of parameters to make sure we got them all 
  145.         (even though we don't want any) and then calls the OpenApp userProc in QSUserProcs.
  146.         Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  147.     }
  148.  
  149.     FUNCTION HandleOAPP(theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  150.     VAR
  151.         errStr: Str255;
  152.  
  153.     BEGIN
  154.         FailErr(GotRequiredParams(theAppleEvent));
  155.  
  156.         { let's show the user the splash screen }
  157.         ShowWindow(gSplashScreen);
  158.  
  159.         OpenApp;     {pass it on to the app specific routine}
  160.  
  161.         IF reply.dataHandle <> NIL THEN { a reply is sought }
  162.         BEGIN
  163.             errStr := 'Opening';
  164.             HandleOAPP := AEPutParamPtr(reply, 'errs', 'TEXT', Ptr(@errStr[1]), length(errStr));
  165.         END
  166.         ELSE
  167.             HandleOAPP := noErr;
  168.     END;
  169.  
  170.     {
  171.         This routine is the handler for the quit (Quit Application) event.
  172.         
  173.         It first checks the number of parameters to make sure we got them all 
  174.         (even though we don't want any) and then calls the QuitApp userProc in QSUserProcs.
  175.         Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  176.     }
  177.  
  178.     FUNCTION HandleQUIT(theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  179.     VAR
  180.         errStr: Str255;
  181.  
  182.     BEGIN
  183.         FailErr(GotRequiredParams(theAppleEvent));
  184.  
  185.         QuitApp;    {pass it on to the app specific routine}
  186.  
  187.         IF reply.dataHandle <> NIL THEN { a reply is sought }
  188.         BEGIN
  189.             errStr := 'Quiting';
  190.             HandleQUIT := AEPutParamPtr(reply, 'errs', 'TEXT', Ptr(@errStr[1]), length(errStr));
  191.         END
  192.         ELSE
  193.             HandleQUIT := noErr;
  194.     END;
  195.  
  196.     {
  197.         This routine is the low level processing routine for both the 
  198.         odoc (Open Document) and pdoc (Print Document) events.
  199.         
  200.         This routine is the key one, since this is how we get the list of
  201.         files/folders/disks to process.  The first thing to do is the get the
  202.         list of files, and then make sure that's all the parameters (should be!).
  203.         We then send call the PreflightDocs routine (from DSUserProcs), process
  204.         each file in the list by calling OpenDoc (again in DSUserProcs), and finally
  205.         call PostflightDocs (you know where) and setting a return value.
  206.     }
  207.  
  208.     FUNCTION _HandleDocs(theAppleEvent, reply: AppleEvent; opening: Boolean): OSErr;
  209.     VAR
  210.         myFSS: FSSpec;
  211.         docList: AEDescList;
  212.         index, itemsInList: LONGINT;
  213.         actualSize: Size;
  214.         keywd: AEKeyword;
  215.         typeCode: descType;
  216.         userDataHandle: Handle;
  217.  
  218.     BEGIN
  219.         FailErr(AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, docList));
  220.         FailErr(GotRequiredParams(theAppleEvent));
  221.  
  222.         IF PreFlightDocs(opening, userDataHandle) THEN {let the app do any preflighting it might need}
  223.         BEGIN
  224.             FailErr(AECountItems(docList, itemsInList));    {count the number of items}
  225.             FOR index := 1 TO itemsInList DO
  226.             BEGIN
  227.                 FailErr(AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize));
  228.                 OpenDoc(@myFSS, opening, userDataHandle);    {call the open routine}
  229.             END;
  230.     
  231.             PostFlightDocs(opening, userDataHandle); {and any cleanup}
  232.     
  233.             _HandleDocs := noErr;
  234.         END
  235.         ELSE
  236.             _HandleDocs := errAEEventNotHandled;    {this tells AEM we didn't handle it}
  237.  
  238.         FailErr(AEDisposeDesc(docList));
  239.     END; { _HandleDocs }
  240.  
  241.  
  242.     {
  243.         This routine is the handler for the odoc (Open Document) event.
  244.         
  245.         The odoc event simply calls the common _HandleDocs routines, which will
  246.         do the dirty work of parsing the AEVT & calling the userProcs.
  247.     }
  248.  
  249.     FUNCTION HandleODOC(theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr; 
  250.     BEGIN
  251.         HandleODOC := _HandleDocs(theAppleEvent, reply, TRUE);    {call the low level routine}
  252.     END; { HandleODOC }
  253.  
  254.     {
  255.         This routine is the handler for the pdoc (Print Document) event.
  256.         
  257.         The pdoc event like the odoc simply calls the common _HandleDocs routines
  258.     }
  259.  
  260.     FUNCTION HandlePDOC(theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr; 
  261.     BEGIN
  262.         HandlePDOC := _HandleDocs(theAppleEvent, reply, FALSE);    {call the low level routine}
  263.     END; { HandlePDOC }
  264.  
  265.     {
  266.         This is the routine called by the main event loop, when a high level
  267.         event is found.  Since we only deal with Apple events, and not other
  268.         high level events, we just pass everything onto the AEM via AEProcessAppleEvent
  269.     }
  270.     PROCEDURE DoHighLevelEvent(event: EventRecord);
  271.     BEGIN
  272.         FailErr(AEProcessAppleEvent(event));
  273.     END;
  274.  
  275. END.